package service

import (
	"gitee.com/lzqysoft/framework/services/base/sql"
	"gitee.com/lzqysoft/framework/services/base/models"
	"gitee.com/lzqysoft/framework/common/operation"
	"gitee.com/lzqysoft/framework/common/encrypt"
	"gitee.com/lzqysoft/framework/common/log"
	"fmt"
)

// InitDB 初始化数据库相关信息
// 清空数据库，并出入初始数据
func InitDB() error {
	db, _ := sql.GetConn()
	session := db.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		fmt.Println(1)
		log.Error(err)
		return err
	}
	organ := getOrgan()
	if _, err = session.Insert(organ);err != nil {
		fmt.Println(2)
		log.Error(err)
		session.Rollback()
		return err
	}
	group := getUserGroup(organ.Id)
	if _, err = session.Insert(group);err != nil {
		fmt.Println(3)
		log.Error(err)
		session.Rollback()
		return err
	}
	module1 := getModule(organ.Id)
	if _, err = session.Insert(module1);err != nil {
		fmt.Println(4)
		log.Error(err)
		session.Rollback()
		return err
	}
	module2 := getSecondModule(module1.Id)
	if _, err = session.Insert(module2);err != nil {
		fmt.Println(5)
		log.Error(err)
		session.Rollback()
		return err
	}
	auths := getAuths(organ.Id,module2.Id, module2.Code)
	if _, err = session.Insert(auths);err != nil {
		fmt.Println(6)
		session.Rollback()
		return err
	}
	user  := getUser(organ.Id, group.Id)
	if _, err = session.Insert(user);err != nil {
		fmt.Println(6)
		session.Rollback()
		return err
	}
	return session.Commit()
}

func getOrgan() *models.Organ {
	organ := &models.Organ{}
	organ.Name = "系统"
	organ.Category = "01"
	organ.Desc = "系统root单位"
	organ.Domain = "www.onstream-cloud.com"
	organ.Context = "/usr/local/framework"
	organ.Pid = "root"
	organ.Onwer = "root"
	organ.Group = "root"
	organ.Creater = "root"
	organ.Updater="root"
	organ.CreateOrg = "root"
	organ.Isdelete = "00"
	organ.Status = "00"
	organ.Operation = operation.GenerateDefaultPermission()


	return organ
}

func getUserGroup(orgId string) *models.Group {
	group := &models.Group{}
	group.Onwer = "root"
	group.Group = "root"
	group.Creater = "root"
	group.Updater = "root"
	group.CreateOrg = orgId
	group.Isdelete = "00"
	group.Status = "00"
	group.Name = "root"
	group.Desc = "系统管理员，拥有任何权限"
	group.OrgId = orgId
	group.Operation = operation.GenerateDefaultPermission()
	return group
}

func getModule(orgId string) *models.Module {
	module := &models.Module{}
	module.Onwer = "root"
	module.Group = "root"
	module.Creater = "root"
	module.Updater = "root"
	module.CreateOrg = orgId
	module.Isdelete = "00"
	module.Status = "00"
	module.Name = "基础模块"
	module.Code = "BASE"
	module.Category = "00"
	module.Node = "root"
	module.Pnode = ""
	module.Pid = "root"
	module.Level = "00"
	module.Operation = operation.GenerateDefaultPermission()
	return module
}
func getSecondModule(pid string) *models.Module{
	module := &models.Module{}
	module.Onwer = "root"
	module.Group = "root"
	module.Creater = "root"
	module.Updater = "root"
	module.CreateOrg = "root"
	module.Isdelete = "00"
	module.Status = "00"
	module.Name = "日志管理"
	module.Code = "BASE_LOG"
	module.Category = "00"
	module.Node = "root"
	module.Pnode = ""
	module.Pid = pid
	module.Level = "00"
	module.Operation = operation.GenerateDefaultPermission()
	return module
}

func getAuths(orgId, moduleId, moduleCode string) []*models.Authority{
	var auths []*models.Authority
	for i := 0; i < 3 ; i++ {
		auth := &models.Authority{}
		auth.Onwer = "root"
		auth.Group = "root"
		auth.Creater = "root"
		auth.Updater = "root"
		auth.CreateOrg = orgId
		auth.Isdelete = "00"
		auth.Status = "00"
		auth.Operation = operation.GenerateDefaultPermission()
		auth.ModuleId = moduleId
		auth.ModuleCode = moduleCode
		auth.Category = "00"
		auths = append(auths, auth)
	}
	auths[0].Code, auths[0].Name = "BASE_LOG_API_READ", "查询日志"
	auths[1].Code, auths[1].Name = "BASE_LOG_API_WRITE", "修改日志"
	auths[2].Code, auths[2].Name = "BASE_LOG_API_EXPORT", "导出日志"

	return auths
}

func getUser(orgId, groupId string) *models.User {
	user := &models.User{}
	user.Onwer = "root"
	user.Group = groupId
	user.Creater = "root"
	user.Updater="root"
	user.CreateOrg = "root"
	user.Isdelete = "00"
	user.Status = "00"
	user.Operation = operation.GenerateDefaultPermission()
	user.Account = "admin"
	user.AccountKey = encrypt.EncodeUserPwd(user.Account, "123456")
	user.OrganId = orgId
	return user
}